/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.opgea.crm.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.opgea.crm.common.SetupsAttributeQualifier;
import com.opgea.crm.common.SetupsModuleQualifier;
import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.domain.entities.Opportunity;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.modal.JsonModelMap;
import com.opgea.crm.services.EmployeeService;
import com.opgea.crm.services.LeadService;
import com.opgea.crm.services.OpportunityService;
import com.opgea.crm.services.SetupService;
import com.opgea.crm.web.form.LeadOppForm;
import com.opgea.crm.web.form.OpportunityForm;
import com.opgea.crm.web.modal.LoggedUser;


/**
 *
 * @author User1
 */
@Controller
@RequestMapping(value="opportunity")
public class OpportunityController {

    @Autowired
    OpportunityService opportunityService;
    @Autowired
	SetupService setupService;
    @Autowired
	LeadService leadService;
    @Autowired
	EmployeeService employeeService;
    
  
	
    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView show(HttpServletRequest request){
        return new ModelAndView("opportunity");
    }
    
    @RequestMapping(value="save", method=RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> save(@RequestBody OpportunityForm form,
			                 @RequestParam(required=true, value="action")String action,
			                 HttpServletRequest request){
    	
    	
    	Opportunity opportunity = new Opportunity();
    	if(action.equalsIgnoreCase("update")){
    		opportunity = opportunityService.find(form.getOpportunityId());
    	}
    	
    	opportunity.setOpportunityTitle(form.getOpportunityTitle());
    	if(StringUtils.isNotEmpty(form.getDealSize())){
    		opportunity.setDealSize(Integer.parseInt(form.getDealSize()));
    	}
    	
    	opportunity.setConfidenceLevel(setupService.findById(form.getOppClosingProbabilityId()));
    	opportunity.setSalesPerson(employeeService.find(form.getSalesPersonId()));
    	
    	if(StringUtils.isNotEmpty(form.getTargetDate())){
    		opportunity.setForecastClosingDate(DateUtil.stringToDate(form.getTargetDate()));
    	}
    	if(StringUtils.isNotEmpty(form.getActualCloseDate())){
    		opportunity.setActualCloseDate(DateUtil.stringToDate(form.getActualCloseDate()));
    	}
    	if(StringUtils.isNotEmpty(form.getCreateDate())){
    		opportunity.setCreatedDate(DateUtil.stringToDate(form.getCreateDate()));
    	}
    	if(StringUtils.isNotEmpty(form.getOpportunityDiscription())){
    		opportunity.setOpportunityDescription(form.getOpportunityDiscription().getBytes());
    	}
    	
    	opportunity.setOpportunityStatus(setupService.findById(form.getStatusId()));
    	opportunity.setStage(setupService.findById(form.getStageId()));
    	
    	if(StringUtils.isNotEmpty(form.getId()))
	    opportunity.setRelatedContact(leadService.findLead(new Long(form.getId())));
	   
    	if(action.equalsIgnoreCase("save")){
        	opportunity.setCreatedBy(LoggedUser.getUser(request));
        	opportunity.setCreatedAt(new Date());
			opportunityService.create(opportunity);
		}
		if(action.equalsIgnoreCase("update")){
			opportunityService.update(opportunity);
		}

	    return JsonModelMap.success().data(opportunity.getId());
       }
    
    @RequestMapping(method = RequestMethod.GET, value="showOpportunityDetails")
	public @ResponseBody
	Map<String, Object> showLeadDetails(@RequestParam("opportunityId")String opportunityId){
    	
    	Opportunity opportunity = opportunityService.find(new Long(opportunityId));
		return JsonModelMap.success().data(new OpportunityForm(opportunity));
	}
    
    @RequestMapping(method = RequestMethod.GET, value="showOppDetailsPopupWindow")
	public @ResponseBody
	Map<String, Object> showOppDetailsPopupWindow(@RequestParam("opportunityId")String opportunityId){
    	
    	Opportunity opportunity = opportunityService.find(new Long(opportunityId));
		return JsonModelMap.success().data(new LeadOppForm(opportunity));
	}
    
    @RequestMapping(value ="/opportunityReqCombo" )
	public @ResponseBody
	Map<String ,Object> issueCombo(@RequestParam(value ="operation") String operation,HttpServletResponse response)
	{
		List<SetupsValue> result = null;
		if(operation.equalsIgnoreCase("confidencelevelTypes"))
			result  = setupService.listSetupValue(SetupsModuleQualifier.Opportunity.ordinal(),SetupsAttributeQualifier.Probability.ordinal());
		if(operation.equalsIgnoreCase("stageTypes"))
			result = setupService.listSetupValue(SetupsModuleQualifier.Opportunity.ordinal(), SetupsAttributeQualifier.Stage.ordinal());
		if(operation.equalsIgnoreCase("statusTypes"))
			result = setupService.listSetupValue(SetupsModuleQualifier.Opportunity.ordinal(), SetupsAttributeQualifier.Status.ordinal());
		return JsonModelMap.success().data(result);
	}
    
   
    @RequestMapping(method=RequestMethod.GET, value="salespersonTypes")
    public @ResponseBody Map<String, Object> salespersonTypes(){
    	return JsonModelMap.success().data(employeeService.findAllEmployeeName());
    } 
    
    
    @RequestMapping(method = RequestMethod.GET, value = "relatedTo")
	public @ResponseBody
	Map<String, Object> relatedToList() {
		return JsonModelMap.success().data(leadService.findAllLeadName());
	}
    
    @RequestMapping(method = RequestMethod.GET, value = "leadSearchCriteriaTypes")
	public @ResponseBody
	Map<String, Object> leadSearchCriteriaList() {
		return JsonModelMap.success().data(leadService.getLeadSearchCriteriaTypes());
	}
    
    @RequestMapping(method = RequestMethod.POST, value="deleteOpportunityDetail")
	public @ResponseBody
	Map<String, Object> deleteOpportunityDetail(@RequestParam("opportunityId")String opportunityId){
		opportunityService.remove(new Long(opportunityId));
		return JsonModelMap.successWithMessage("success");
	}
	

}
